import config from "./config.js";
import store from "../store/index.js";

export const request = async (url, data = {}, method) => {
	// const header = {
	// 	'x_own_token': uni.getStorageSync('token'),
	// 	"Content-Type": 'application/x-www-form-urlencoded',
	// };
	if (localStorage.getItem("HASLOGIN") != "1") {
		// 去登陆
		uni.navigateTo({
			url: '/pages/observe/loginView'
		})
	}
	// alert(store.token)
	const header = {
		// "Authorization": 'Bearer ' + uni.getStorageSync("token"),
		"Content-Type": "application/json",
		'x-api-key': localStorage.getItem("TOKEN"),
		'Accept': 'application/json'
	};

	// 默认传值
	// data.platform = config.wechat
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: (res) => {
				resolve(res.data)
			},
			// dataType: config.dataType || 'string',
			complete: (res) => {
				console.log("url--", method)
				console.log("url--", config.Api + url)
				console.log("url--", data)
				console.log("result--", JSON.stringify(res))
				if (res.statusCode === 200) {
					if (res.data.code == 200) {
						resolve(res.data);
						// uni.showToast({
						// 	icon: "none",
						// 	title: res.data.massage,
						// });
						// token失效							// 退出
						// if (res.data.code === 201) {
						// 	logOut()
						// }
					} else if (res.data.code == 500) {
						// 业务错误

						// uni.showToast({
						// 	icon: "none",
						// 	title: res.data.data.msg,
						// });
						reject(res.data)
					} else if (res.data.code == 506) {
						// 当前会话未登录
						reject(res.data)
					} else {
						reject(res.data)
					}

				} else {
					reject(res.data)
					// reject({
					// 	code: 9999,
					// 	msg: "error",
					// });
					// uni.showToast({
					// 	icon: "error",
					// 	title: res.data.error,
					// });
				}
			},
		});
	});
};

export const post = (url, data) => {
	return request(url, data, "POST");
};

export const put = (url, data) => {
	return request(url, data, "PUT");
};

export const del = (url, data) => {
	return request(url, data, "DELETE");
};

export const get = (url, data) => {
	return request(url, data, "GET");
};



export const upload = async (url, filePath) => {
	const headerUpload = {
		// 'x_own_token': uni.getStorageSync('token'),
		'Authorization': 'Bearer ' + uni.getStorageSync("token"),
		"Content-Type": 'application/x-www-form-urlencoded',
	};
	return new Promise((resolve) => {
		uni.uploadFile({
			url: config.Api + url,
			filePath: filePath,
			name: "file",
			formData: {},
			header: headerUpload,
			complete(e) {
				if (e.statusCode === 200) {
					const json = JSON.parse(e.data);
					if (json.code === 200) {
						resolve(json.data);
					} else {
						uni.showToast({
							icon: "none",
							title: json.msg,
						});
						resolve("");
					}
				} else {
					uni.showToast({
						icon: "none",
						title: "上传图片失败",
					});
					resolve("");
				}
			},
		});
	});
};

export const requestSubscribeMessage = async (tmpIds) => {
	return new Promise((resolve) => {
		uni.requestSubscribeMessage({
			tmplIds: tmpIds,
			complete: async (res) => {
				resolve(res);
			},
		});
	});
};


/**
 * 删除文件，参数为FORMDATA
 */
export const dele = async (url, data) => {
	const header = {
		'Authorization': 'Bearer ' + uni.getStorageSync("token"),
		"Content-Type": 'application/x-www-form-urlencoded',
	};
	return new Promise((resolve) => {
		uni.request({
			url: config.Api + url,
			data,
			header: header,
			method: "DELETE",
			complete(e) {
				if (e.statusCode === 200) {
					const json = JSON.parse(e.data);
					if (json.code === 200) {
						resolve(json.data);
					} else {
						uni.showToast({
							icon: "none",
							title: json.msg,
						});
						resolve("");
					}
				} else {
					uni.showToast({
						icon: "none",
						title: "删除失败",
					});
					resolve("");
				}
			},
		});
	});
};